//
// main.cpp for  in /home/jousse_f//workspace/C++/Nibler
// 
// Made by florian jousseau
// Login   <jousse_f@epitech.net>
// 
// Started on  Fri Mar  2 11:15:16 2012 florian jousseau
// Last update Fri Mar  9 18:01:15 2012 florian jousseau
//

#include <stdio.h>
#include <iostream>
#include <SFML/Graphics.hpp>
#include "202loi.h"

using namespace sf;

#define WMAXX	1000
#define WMAXY	600

void	option1(t_loi *loi, RenderWindow & app)
{
  Shape		col[9];
  Shape		line[9];
  int		x = 50;
  int		y = 100;
  int		i = 0;
  int		increm = 5;

  while (i < 8)
    {
      increm = 5;
      if (i != 0 && i != 7)
	increm = 2;
      col[i].AddPoint(x, 100, Color(255, 255, 255), Color(255, 255,255));
      col[i].AddPoint(x, 545, Color(255, 255, 255), Color(255,255,255));
      col[i].AddPoint(x + increm, 545, Color(255, 255, 255), Color(255,255,255));
      col[i].AddPoint(x + increm, 100, Color(255, 255, 255), Color(255,255,255));
      col[i].EnableFill(true);
      x += 127;
      app.Draw(col[i]);
      i++;
    }
  i = 0;
  while (i < 8)
    {
      increm = 5;
      if (i != 0 && i != 7)
	increm = 2;
      line[i].AddPoint(50, y, Color(255, 255, 255), Color(255, 255,255));
      line[i].AddPoint(50, y + increm, Color(255, 255, 255), Color(255,255,255));
      line[i].AddPoint(940, y + increm, Color(255, 255, 255), Color(255,255,255));
      line[i].AddPoint(940, y, Color(255, 255, 255), Color(255,255,255));
      line[i].EnableFill(true);
      y += 63;
      app.Draw(line[i]);
      i++;
    }
  int		posX = 100;
  int		posY = 50;
  String	in;
  x = 0;
  while (x < 7)
    {
      posY = 50;
      y = 0;
      while (y < 7)
	{
	  in.SetText(loi->tab->tab1->getSTab()[x][y]);
	  in.SetFont(loi->font);
	  in.SetSize(30);
	  int lg = in.GetRect().GetWidth();
	  int h = in.GetRect().GetHeight();
	  in.SetPosition(Vector2f(((127 - lg) / 2) + posY , ((63 - h) / 2) + posX));
	  app.Draw(in);
	  y++;
	  posY += 127;
	}
      x++;
      posX += 63;
    }
  loi->text.SetText("Option 1 :");
  loi->text.SetColor(Color(255, 0, 0));
}

void	option2(t_loi *loi, RenderWindow & app)
{
  Shape		col[4];
  Shape		line[4];
  int		x = 50;
  int		y = 100;
  int		i = 0;
  int		increm = 5;

  while (i < 4)
    {
      increm = 5;
      if (i != 0 && i != 3)
	increm = 2;
      col[i].AddPoint(x, 100, Color(255, 255, 255), Color(255, 255,255));
      col[i].AddPoint(x, 545, Color(255, 255, 255), Color(255,255,255));
      col[i].AddPoint(x + increm, 545, Color(255, 255, 255), Color(255,255,255));
      col[i].AddPoint(x + increm, 100, Color(255, 255, 255), Color(255,255,255));
      col[i].EnableFill(true);
      x += 296;
      app.Draw(col[i]);
      i++;
    }
  i = 0;
  while (i < 4)
    {
      increm = 5;
      if (i != 0 && i != 3)
	increm = 2;
      line[i].AddPoint(50, y, Color(255, 255, 255), Color(255, 255,255));
      line[i].AddPoint(50, y + increm, Color(255, 255, 255), Color(255,255,255));
      line[i].AddPoint(940, y + increm, Color(255, 255, 255), Color(255,255,255));
      line[i].AddPoint(940, y, Color(255, 255, 255), Color(255,255,255));
      line[i].EnableFill(true);
      y += 147;
      app.Draw(line[i]);
      i++;
    }
  float		posX = 100;
  int		posY = 50;
  String	in;
  x = 0;
  while (x < 3)
    {
      posY = 50;
      y = 0;
      while (y < 3)
	{
	  in.SetText(loi->tab->tab2->getSTab()[x][y]);
	  in.SetFont(loi->font);
	  in.SetSize(60);
	  int lg = in.GetRect().GetWidth();
	  int h = in.GetRect().GetHeight();
	  in.SetPosition(Vector2f(((296 - lg) / 2) + posY , ((147 - h) / 2) + posX));
	  app.Draw(in);
	  y++;
	  posY += 296;
	}
      x++;
      posX += 147;
    }
  loi->text.SetText("OPTION 2 :");
  loi->text.SetColor(Color(0, 0, 255));
}

void	option3(t_loi *loi, RenderWindow & app)
{
  Shape		col[12];
  Shape		line[3];
  int		x = 10;
  int		y = 275;
  int		i = 0;
  int		increm = 5;

  while (i < 12)
    {
      increm = 5;
      if (i != 0 && i != 11)
	increm = 2;
      col[i].AddPoint(x, 275, Color(255, 255, 255), Color(255, 255,255));
      col[i].AddPoint(x, 375, Color(255, 255, 255), Color(255,255,255));
      col[i].AddPoint(x + increm, 375, Color(255, 255, 255), Color(255,255,255));
      col[i].AddPoint(x + increm, 275, Color(255, 255, 255), Color(255,255,255));
      col[i].EnableFill(true);
      x += 89;
      app.Draw(col[i]);
      i++;
    }
  i = 0;
  while (i < 3)
    {
      increm = 5;
      if (i != 0 && i != 2)
  	increm = 2;
      line[i].AddPoint(10, y, Color(255, 255, 255), Color(255, 255,255));
      line[i].AddPoint(10, y + increm, Color(255, 255, 255), Color(255,255,255));
      line[i].AddPoint(994, y + increm, Color(255, 255, 255), Color(255,255,255));
      line[i].AddPoint(994, y, Color(255, 255, 255), Color(255,255,255));
      line[i].EnableFill(true);
      y += 50;
      app.Draw(line[i]);
      i++;
    }
  int		posX = 275;
  int		posY = 10;
  String	in;
  x = 0;
  while (x < 2)
    {
      posY = 10;
      y = 0;
      while (y < 11)
  	{
  	  in.SetText(loi->tab->tab3->getSTab()[x][y]);
  	  in.SetFont(loi->font);
  	  in.SetSize(30);
  	  int lg = in.GetRect().GetWidth();
  	  int h = in.GetRect().GetHeight();
  	  in.SetPosition(Vector2f(((89 - lg) / 2) + posY , ((50 - h) / 2) + posX));
  	  app.Draw(in);
  	  y++;
  	  posY += 89;
  	}
      x++;
      posX += 50;
    }
  loi->text.SetText("OPTION 3 :");
  loi->text.SetColor(Color(0, 255, 0));
}

void	option4(t_loi *loi, RenderWindow & app)
{
  Shape		col[11];
  Shape		line[3];
  int		x = 10;
  int		y = 275;
  int		i = 0;
  int		increm = 5;

  while (i < 11)
    {
      increm = 5;
      if (i != 0 && i != 10)
	increm = 2;
      col[i].AddPoint(x, 275, Color(255, 255, 255), Color(255, 255,255));
      col[i].AddPoint(x, 375, Color(255, 255, 255), Color(255,255,255));
      col[i].AddPoint(x + increm, 375, Color(255, 255, 255), Color(255,255,255));
      col[i].AddPoint(x + increm, 275, Color(255, 255, 255), Color(255,255,255));
      col[i].EnableFill(true);
      x += 98;
      app.Draw(col[i]);
      i++;
    }
  i = 0;
  while (i < 3)
    {
      increm = 5;
      if (i != 0 && i != 2)
  	increm = 2;
      line[i].AddPoint(10, y, Color(255, 255, 255), Color(255, 255,255));
      line[i].AddPoint(10, y + increm, Color(255, 255, 255), Color(255,255,255));
      line[i].AddPoint(995, y + increm, Color(255, 255, 255), Color(255,255,255));
      line[i].AddPoint(995, y, Color(255, 255, 255), Color(255,255,255));
      line[i].EnableFill(true);
      y += 50;
      app.Draw(line[i]);
      i++;
    }
  int		posX = 275;
  int		posY = 10;
  String	in;
  x = 0;
  while (x < 2)
    {
      posY = 10;
      y = 0;
      while (y < 10)
  	{
  	  in.SetText(loi->tab->tab4->getSTab()[x][y]);
  	  in.SetFont(loi->font);
  	  in.SetSize(30);
  	  int lg = in.GetRect().GetWidth();
  	  int h = in.GetRect().GetHeight();
  	  in.SetPosition(Vector2f(((98 - lg) / 2) + posY , ((50 - h) / 2) + posX));
  	  app.Draw(in);
  	  y++;
  	  posY += 98;
  	}
      x++;
      posX += 50;
    }
  loi->text.SetText("OPTION 4 :");
  loi->text.SetColor(Color(255, 255, 0));
}

void	option5(t_loi *loi, RenderWindow & app)
{
  Shape		col[4];
  Shape		line[3];
  int		x = 100;
  int		y = 100;
  int		i = 0;
  int		increm = 5;

  while (i < 4)
    {
      increm = 5;
      if (i != 0 && i != 3)
	increm = 2;
      col[i].AddPoint(x, 100, Color(255, 255, 255), Color(255, 255,255));
      col[i].AddPoint(x, 545, Color(255, 255, 255), Color(255,255,255));
      col[i].AddPoint(x + increm, 545, Color(255, 255, 255), Color(255,255,255));
      col[i].AddPoint(x + increm, 100, Color(255, 255, 255), Color(255,255,255));
      col[i].EnableFill(true);
      x += 265;
      app.Draw(col[i]);
      i++;
    }
  i = 0;
  while (i < 3)
    {
      increm = 5;
      if (i != 0 && i != 2)
	increm = 2;
      line[i].AddPoint(100, y, Color(255, 255, 255), Color(255, 255,255));
      line[i].AddPoint(100, y + increm, Color(255, 255, 255), Color(255,255,255));
      line[i].AddPoint(900, y + increm, Color(255, 255, 255), Color(255,255,255));
      line[i].AddPoint(900, y, Color(255, 255, 255), Color(255,255,255));
      line[i].EnableFill(true);
      y += 222;
      app.Draw(line[i]);
      i++;
    }
  int		posX = 100;
  int		posY = 100;
  String	in;
  x = 0;
  while (x < 2)
    {
      posY = 100;
      y = 0;
      while (y < 3)
  	{
  	  in.SetText(loi->tab->tab5->getSTab()[x][y]);
  	  in.SetFont(loi->font);
  	  in.SetSize(50);
  	  int lg = in.GetRect().GetWidth();
  	  int h = in.GetRect().GetHeight();
  	  in.SetPosition(Vector2f(((265 - lg) / 2) + posY , ((222 - h) / 2) + posX));
  	  app.Draw(in);
  	  y++;
  	  posY += 265;
  	}
      x++;
      posX += 222;
    }
    loi->text.SetText("Option 5 :");
    loi->text.SetColor(Color(0, 255, 255));
}

void	aff_option(t_loi *loi, RenderWindow & app)
{
  app.Clear();
  if (!loi->font.LoadFromFile("DS-DIGIB.TTF"))
    {
      printf("ERREUR CHARGEMENT POLICE\n");
    }
  loi->text.SetFont(loi->font);
  loi->text.SetSize(50);
  int largeurTexte = loi->text.GetRect().GetWidth();
  loi->text.SetPosition(Vector2f((WMAXX - largeurTexte) / 2, 10));
  if (loi->option == 1)
    option1(loi, app);
  if (loi->option == 2)
    option2(loi, app);
  if (loi->option == 3)
    option3(loi, app);
  if (loi->option == 4)
    option4(loi, app);
  if (loi->option == 5)
    option5(loi, app);
  app.Draw(loi->text);
  app.Display();
}

int	start_graph(t_tab *tab, int op)
{
  RenderWindow		app(VideoMode(WMAXX, WMAXY, 32), "202LOI");
  t_loi			loi;
  Event			event;

  loi.tab = tab;
  loi.option = op;
  while (app.IsOpened())
    {
      app.GetEvent(event);
      switch (event.Type)
	{
	case Event::Closed :
	  app.Close();
	  break;
	case Event::KeyPressed :
	  {
	    switch (event.Key.Code)
	      {
	      case Key::Escape :
		app.Close();
		break;
	      }
	  }
	  break;
	}
      const Input & input = app.GetInput();
      if (input.IsKeyDown(Key::Num1))
	loi.option = 1;
      if (input.IsKeyDown(Key::Num2))
	loi.option = 2;
      if (input.IsKeyDown(Key::Num3))
	loi.option = 3;
      if (input.IsKeyDown(Key::Num4))
	loi.option = 4;
      if (input.IsKeyDown(Key::Num5))
	loi.option = 5;
      aff_option(&loi, app);
    }
  return EXIT_SUCCESS;
}
